﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using Hxsoft.AppV1.Module;

namespace Hxsoft.AppV1.Admin.LinkManage.Controls
{
	public partial class Link : BaseUserControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
			this.DataBind();
		}

		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);

			FCKeditor.BindUploadUrl(CtrlLinkWords, GlobalID, Links.INFO_TYPE, SiteId);
		}	

		/// <summary>
		/// 当前修改信息的标识符。
		/// </summary>
		protected int? LinkID
		{
			get
			{
				return IntExtensions.ParseOrNull(Request.QueryString["LinkID"]);
			}
		}


		#region 资源相关	

		/// <summary>
		/// 上传图片的类型。
		/// </summary>
		public string ImageType
		{
			get
			{
				return Convert.ToString(ImageUploader.Value);
			}
			set
			{
				ImageUploader.Value = value;
			}
		}
		#endregion


		/// <summary>
		/// 属性 Title
		/// </summary>
		public string Title
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlTitle.Text))
					return null;

				return Convert.ToString(CtrlTitle.Text);
			}
			set
			{
				if (value == null)
					CtrlTitle.Text = "";
				else
					CtrlTitle.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 LinkWords
		/// </summary>
		public string LinkWords
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlLinkWords.Value))
					return null;

				return Convert.ToString(CtrlLinkWords.Value);
			}
			set
			{
				if (value == null)
					CtrlLinkWords.Value = "";
				else
					CtrlLinkWords.Value = value.ToString();
			}
		}

		/// <summary>
		/// 属性 LinkUrl
		/// </summary>
		public string LinkUrl
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlLinkUrl.Text))
					return null;

				return Convert.ToString(CtrlLinkUrl.Text);
			}
			set
			{
				if (value == null)
					CtrlLinkUrl.Text = "";
				else
					CtrlLinkUrl.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 StartDate
		/// </summary>
		public DateTime StartDate
		{
			get
			{
				return Convert.ToDateTime(CtrlStartDate.Text);
			}
			set
			{
				CtrlStartDate.Text = value.ToString();
			}
		}

		/// <summary>
		/// 属性 EndDate
		/// </summary>
		public DateTime? EndDate
		{
			get
			{
				if (String.IsNullOrEmpty(CtrlEndDate.Text))
					return null;

				return Convert.ToDateTime(CtrlEndDate.Text);
			}
			set
			{
				if (value == null)
					CtrlEndDate.Text = "";
				else
					CtrlEndDate.Text = value.ToString();
			}
		}

		public string ChannelIds
		{
			get
			{
				return StringExtensions.CasingCompound<string>(CBLChannelIds.Values, Companies.OperationTypeSeparator);
			}
			set
			{
				if (!string.IsNullOrEmpty(value))
				{
					CBLChannelIds.DataBind();
					CBLChannelIds.Values = StringExtensions.CasingDecompound<string>(value, Companies.OperationTypeSeparator);
				}
			}
		}


		/// <summary>
		/// 控件的 LinkInfo 实例。
		/// </summary>
		public Module.Link Info
		{
			get
			{
				Module.Link link = new Module.Link();

				link.Title = Title;
				link.ImageType = ImageType;
				link.LinkWords = LinkWords;
				link.LinkUrl = LinkUrl;
				link.StartDate = StartDate;
				link.EndDate = EndDate;
				link.GlobalId = GlobalID;
				link.ChannelIds = ChannelIds;

				return link;
			}
			set
			{
				if (value == null) return;

				Title = value.Title;
				ImageType = value.ImageType;
				LinkWords = value.LinkWords;
				LinkUrl = value.LinkUrl;
				StartDate = value.StartDate;
				EndDate = value.EndDate;
				GlobalID = value.GlobalId;
				ChannelIds = value.ChannelIds;
			}
		}
	}
}